var stopsave = 0;
var layoutdata;//缓存布局数据变量
var layoutdataObj = {};
var cleanHtmlObj = {};//存储cleanHtml数组
var currentDocument = null;
var timerSave = 2e3;
var demoHtml = $(".demo").html();
var currenteditor = null;//当前要编辑的对象
var pTitle = "";//项目标题对象
var pSubTitle = "";//项目小标题对象
var titleHide = "";//隐藏标题栏对象
var imgHide = "";//文章图片是否隐藏
var pageID = 1;//当前页面ID
var pageIDsArr = [];
var timer = "";//存储缓存数据计时器
var layouthistory;
//获取域名参数方法
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
//获取参数的值
function getParam(url,name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = url.split("?")[1].match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
function supportstorage() {
    if (typeof window.localStorage=='object')
        return true;
    else
        return false;
}
function handleSaveLayout() {

    var htmlStr = $(".demo").html();
    var reg=/<script[^>]*>.*(?=<\/script>)<\/script>/gi;
    var scriptArr = htmlStr.match(reg);
    $("#download-layout").children().html(htmlStr.replace(reg,""));

    $("#download-layout").children().find(".marquee").children(".txt").each(function(){
        $(this).html($(this).parent(".marquee").siblings(".marqueeHidden").html());
    });
    var e = $("#download-layout").children().html();
    if(scriptArr != null){
        for(var i = 0; i < scriptArr.length; i++){
            e += scriptArr[i];
        }
    }

    //var e = $(".demo").html();
    if (!stopsave && e != window.demoHtml) {
        stopsave++;
        window.demoHtml = e;
        saveLayout(pageID);
        stopsave--;
        layoutdataObj[pageID] = e;
    }
}
function saveLayout(pageID){
    var data = JSON.parse(localStorage.getItem("layoutdata"+pageID));
    if (!data) {
        data={};
        data.count = 0;
        data.list = [];
    }
    if (data.list.length>data.count) {
        for (i=data.count;i<data.list.length;i++)
            data.list[i]=null;
    }

    data.list[data.count] = window.demoHtml;
    data.count++;
    if (supportstorage()) {
        localStorage.setItem("layoutdata"+pageID,JSON.stringify(data));
    }
    layouthistory = data;
}
//获取获取页面html
function getPageHtml(siteid,pageid){
    $.ajax({
        url:"/action/web_cmd.asp?act=page_html",
        type:"post",
        dataType:"html",
        asycn:false,
        data:{
            siteid:siteid,
            pageid:pageid
        },
        success:function(data){
            layoutdataObj[pageid] = data;
            var htmlData = {};
            htmlData.count = 0;
            htmlData.list = [];
            htmlData.list[htmlData.count] = data;
            htmlData.count++;
            if (supportstorage()) {
                localStorage.setItem("layoutdata"+pageid,JSON.stringify(htmlData));
            }
            if(pageid == 1){
                $(".demo").html(data);
                //开始滚动
                scrollTxt();
                //----------------拖动功能---------------
                dragComponent();
                //-------------写到缓存计时器------------
                timer = setInterval(function () {
                    handleSaveLayout()
                }, timerSave)
            }
        }
    })
}
function restoreData(){
    if (supportstorage()) {
        layouthistory = JSON.parse(localStorage.getItem("layoutdata"+pageID));
        if(!layouthistory){
            $(".demo").html("");
        }else{
            window.demoHtml = layouthistory.list[layouthistory.count-1];
            if (window.demoHtml) $(".demo").html(window.demoHtml);
        }
    }
}
//获取干净的html
function downloadLayoutSrc(htmlStr) {
    var e = "";
    var reg=/<script[^>]*>.*(?=<\/script>)<\/script>/gi;
    var scriptArr = htmlStr.match(reg);
    $("#download-layout").children().html(htmlStr.replace(reg,""));
    var t = $("#download-layout").children();
    t.find(".preview, .configuration, .drag, .remove").remove();
    t.find(".lyrow").addClass("removeClean");
    t.find(".box-element").addClass("removeClean");
    t.find(".lyrow .lyrow .lyrow .lyrow .lyrow .removeClean").each(function () {
        cleanHtml(this)
    });
    t.find(".lyrow .lyrow .lyrow .lyrow .removeClean").each(function () {
        cleanHtml(this)
    });
    t.find(".lyrow .lyrow .lyrow .removeClean").each(function () {
        cleanHtml(this)
    });
    t.find(".lyrow .lyrow .removeClean").each(function () {
        cleanHtml(this)
    });
    t.find(".lyrow .removeClean").each(function () {
        cleanHtml(this)
    });
    t.find(".removeClean").each(function () {
        cleanHtml(this)
    });
    t.find(".removeClean").remove();
    $("#download-layout .column").removeClass("ui-sortable");
    $("#download-layout .row-fluid").removeClass("clearfix").children().removeClass("column");
    if ($("#download-layout .container").length > 0) {
        changeStructure("row-fluid", "row")
    }
    // formatSrc = $.htmlClean($("#download-layout").html(), {
    //     format: true,
    //     allowedAttributes: [
    //         ["id"],
    //         ["class"],
    //         ["data-toggle"],
    //         ["data-target"],
    //         ["data-parent"],
    //         ["role"],
    //         ["data-dismiss"],
    //         ["aria-labelledby"],
    //         ["aria-hidden"],
    //         ["data-slide-to"],
    //         ["data-slide"]
    //     ]
    // });
    //$("#download-layout").html(formatSrc);
    //$("#download-layout").html();
    var str = $("#download-layout").html();
    if(scriptArr != null){
        for(var i = 0; i < scriptArr.length; i++){
            str += scriptArr[i];
        }
    }
    return str;
}
function handleJsIds() {
    handleModalIds();
    handleAccordionIds();
    handleAritcleIds();
    handleAlbumIds();
    handleCarouselIds();
    handleTabsIds()
}
function handleAccordionIds() {
    var e = $(".demo #myAccordion");
    var t = randomNumber();
    var n = "panel-" + t;
    var r;
    e.attr("id", n);
    e.find(".panel").each(function (e, t) {
        r = "panel-element-" + randomNumber();
        $(t).find(".panel-title").each(function (e, t) {
            $(t).attr("data-parent", "#" + n);
            $(t).attr("href", "#" + r)
        });
        $(t).find(".panel-collapse").each(function (e, t) {
            $(t).attr("id", r)
        })
    })
}
function handleCarouselIds() {
    var e = $(".demo #myCarousel");
    var t = randomNumber();
    var n = "carousel-" + t;
    e.attr("id", n);
    e.find(".carousel-indicators li").each(function (e, t) {
        $(t).attr("data-target", "#" + n)
    });
    e.find(".left").attr("href", "#" + n);
    e.find(".right").attr("href", "#" + n)
}
function handleAritcleIds(){
    var e = $(".demo #myArticle");
    var t = randomNumber();
    var n = "article-"+t;
    e.attr("id",n);
}
function handleAlbumIds(){
    var e = $(".demo #myAlbum");
    var t = randomNumber();
    var n = "album-"+t;
    e.attr("id",n);
}
function handleModalIds() {
    var e = $(".demo #myModalLink");
    var t = randomNumber();
    var n = "modal-container-" + t;
    var r = "modal-" + t;
    e.attr("id", r);
    e.attr("href", "#" + n);
    e.next().attr("id", n)
}
function handleTabsIds() {
    var e = $(".demo #myTabs");
    var t = randomNumber();
    var n = "tabs-" + t;
    e.attr("id", n);
    e.find(".tab-pane").each(function (e, t) {
        var n = $(t).attr("id");
        var r = "panel-" + randomNumber();
        $(t).attr("id", r);
        $(t).parent().parent().find("a[href=#" + n + "]").attr("href", "#" + r)
    })
}
function randomNumber() {
    return randomFromInterval(1, 1e6)
}
function randomFromInterval(e, t) {
    return Math.floor(Math.random() * (t - e + 1) + e)
}
function gridSystemGenerator() {
    $(".lyrow .preview input").bind("keyup", function () {
        var e = 0;
        var t = "";
        var n = false;
        var r = $(this).val().split(" ", 12);
        $.each(r, function (r, i) {
            if (!n) {
                if (parseInt(i) <= 0) n = true;
                e = e + parseInt(i);
                t += '<div class="col-md-' + i + ' column"></div>'
            }
        });
        if (e == 12 && !n) {
            $(this).parent().next().children().html(t);
            $(this).parent().prev().show()
        } else {
            $(this).parent().prev().hide()
        }
    })
}
function configurationElm(e, t) {
    $(".demo").delegate(".configuration > a", "click", function (e) {
        e.preventDefault();
        var t = $(this).parent().next().next().children();
        $(this).toggleClass("active");
        t.toggleClass($(this).attr("rel"))
    });
    $(".demo").delegate(".configuration .dropdown-menu a", "click", function (e) {
        e.preventDefault();
        var t = $(this).parent().parent();
        var n = t.parent().parent().next().next().children();
        t.find("li").removeClass("active");
        $(this).parent().addClass("active");
        var r = "";
        t.find("a").each(function () {
            r += $(this).attr("rel") + " "
        });
        t.parent().removeClass("open");
        n.removeClass(r);
        n.addClass($(this).attr("rel"))
    })
}
function removeElm() {
    $(".demo").delegate(".remove", "click", function (e) {
        e.preventDefault();
        if($(this).parent().find(".articleList").length > 0){
            var curID = $(this).parent().find(".articleList").attr("id");
            $("#script-"+curID).remove();
        }
        if($(this).parent().find(".albumList").length > 0){
            var curID = $(this).parent().find(".albumList").attr("id");
            $("#script-"+curID).remove();
        }
        $(this).parent().remove();
        if (!$(".demo .lyrow").length > 0) {
            clearDemo()
        }
    })
}
function clearDemo() {
    $(".demo").empty()
}
function removeMenuClasses() {
    $("#menu-layoutit li button").removeClass("active")
}
function changeStructure(e, t) {
    $("#download-layout ." + e).removeClass(e).addClass(t)
}
function cleanHtml(e) {
    $(e).parent().append($(e).children().html())
}
//调用滚动文字
function scrollTxt(){
    $(".demo").find(".marquee").each(function(){
        $(this).kxbdMarquee({direction:"up",isEqual:false});
    });
}
//拖动组件
function dragComponent(){
    $(".demo, .demo .column").sortable({
        connectWith: ".column",
        opacity: .35,
        handle: ".drag"
    });
    $(".sidebar-nav .lyrow").draggable({
        connectToSortable: ".demo",
        helper: "clone",
        handle: ".drag",
        drag: function (e, t) {
            t.helper.width(400);
        },
        stop: function (e, t) {
            $(".demo .column").sortable({
                opacity: .35,
                connectWith: ".column",
            });
            //row不能嵌套row
            var lyObj = $(".demo").find(".column").find(".lyrow");
            if(lyObj.length > 0){
                lyObj.remove();
            }
        }
    });
    $(".sidebar-nav .box").draggable({
        connectToSortable: ".column",
        helper: "clone",
        handle: ".drag",
        drag: function (e, t) {
            t.helper.width(400);
        },
        stop: function () {
            handleJsIds();
            if($(this).find(".marquee").length > 0){
                $(".demo").find(".marquee").find(".txt").each(function(){
                    if(typeof($(this).attr("style")) == "undefined"){
                        $(this).parent(".marquee").kxbdMarquee({direction:"up",isEqual:false});
                    }
                })
            }
        }
    });
}
//上传图片
function uploadImg(ele){

}
function showFullCarouse(pageid){
    //显示全屏轮播
    var isShowFullCarousel = false;
    $.ajax({
        url:"/action/web_cmd.asp?act=qpt_show",
        type:"post",
        data:{
            pageid:pageid
        },
        success:function(data) {
            var jsondata = $.parseJSON(data);
            if(jsondata.status == 0){
                var num = 0;
                var olHtml = '<ol class="carousel-indicators">';
                var divHtml = '<div class="carousel-inner">';
                var aHtml = '<a class="left carousel-control" href="#fullCarousel" data-slide="prev">'+
                    '<span class="glyphicon glyphicon-chevron-left"></span>'+
                    '</a>'+
                    '<a class="right carousel-control" href="#fullCarousel" data-slide="next">'+
                    '<span class="glyphicon glyphicon-chevron-right"></span>'+
                    '</a>';
                for(var i = 0; i < jsondata.piclist.length; i++){
                    if(jsondata.piclist[i].tu != ""){
                        isShowFullCarousel = true;
                        if(num == 0){
                            olHtml += '<li class="active" data-slide-to="'+num+'" data-target="#fullCarousel"></li>';
                            divHtml += '<div class="item active"><a href="'+jsondata.piclist[i].link+'"><img src="'+jsondata.piclist[i].tu+'"></a></div>';
                            num++;
                        }else{
                            olHtml += '<li data-slide-to="'+num+'" data-target="#fullCarousel"></li>';
                            divHtml += '<div class="item"><a href="'+jsondata.piclist[i].link+'"><img src="'+jsondata.piclist[i].tu+'"></a></div>';
                        }
                    }
                }
                olHtml += '</ol>';
                divHtml +='</div>';
            }
            if(isShowFullCarousel){
                $("#fullCarousel").html(olHtml+divHtml+aHtml);
                $('#fullCarousel').carousel();
            }else{
                $("#fullCarousel").empty();
            }
        }
    });
}
$(window).resize(function () {
    $("body").css("min-height", $(window).height() - 90);
    $(".demo").css("min-height", $(window).height() - 160);
    if($("body").width() > 1200){
        $(".outerBox").width(1140);
    }else if($("body").width() > 992){
        $(".outerBox").width(940);
    }else if($("body").width() > 768){
        $(".outerBox").width(720);
    }else{
        $(".outerBox").width($("body").width() - 30);
        $(".suspension").width($("body").width());
    }
});
//--------------------------页面初始化加载--------------------------
$(document).ready(function () {
    //simditor编辑器
    var editor = new Simditor({
        textarea: $('#editor'),
        toolbar:['title','bold','italic','underline','strikethrough','color','ol','ul','blockquote','code','link','alignment']
    });
    $("body").css("min-height", $(window).height() - 90);
    $(".demo").css("min-height", $(window).height() - 160);
    if($("body").width() > 1200){
        $(".outerBox").width(1140);
    }else if($("body").width() > 992){
        $(".outerBox").width(940);
    }else if($("body").width() > 768){
        $(".outerBox").width(720);
    }else{
        $(".outerBox").width($("body").width() - 30);
        $(".suspension").width($("body").width());
    }
    //悬浮框宽度调整
    $(window).resize(function () {
        $(".suspension").width($("body").width());
    });
    showFullCarouse(1);//显示全屏轮播
    //左侧菜单栏切换
    $(".left-panel .left-hand a").click(function (e) {
        e.preventDefault();
        var index = $(this).index();
        $(this).addClass("choosem").siblings("a").removeClass("choosem");
        $(".sidebar-nav").eq(index).removeClass("hidden").siblings(".sidebar-nav").addClass("hidden");
    });
    //-----------------顶部底部背景相关设置-----------------
    $('#headBgUploader').fileupload({
        url: '/action/upload/',
        dataType: 'json',
        add: function (e, data) {
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { fileinpname: 'pic', time: Date() }
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.files[0].name;
                //filename = filename.substring(0,filename.length-4);
                $("#headBgImg").attr("src",data.result.relatPath);
            }
        }
    });
    $('#footBgUploader').fileupload({
        url: '/action/upload/',
        dataType: 'json',
        add: function (e, data) {
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { fileinpname: 'pic', time: Date() }
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.files[0].name;
                //filename = filename.substring(0,filename.length-4);
                $("#footBgImg").attr("src",data.result.relatPath);
            }
        }
    });
    //top_bg=顶部背景颜色|背景图片路径|平铺|方向&bot_bg=底部背景颜色|背景图片路径|平铺|方向
    $("#headFootBgSave").click(function(){
        $.ajax({
            url:"/action/web_cmd.asp?act=set_bg",
            type:"post",
            data:{
                top_bg:$("#headBg").spectrum("get")+"|"+$("#headBgImg").attr("src")+"|"+$("#headBgRepeat").val()+"|"+$("#headBgPosition").val(),
                bot_bg:$("#footBg").spectrum("get")+"|"+$("#footBgImg").attr("src")+"|"+$("#footBgRepeat").val()+"|"+$("#footBgPosition").val()
            },
            success:function(data){
                if(data.status == 0){
                    $("#header").css("background",$("#headBg").spectrum("get")+" url("+$("#headBgImg").attr("src")+") "+$("#headBgRepeat").val()+" "+$("#headBgPosition").val());
                    $("#footer").css("background",$("#footBg").spectrum("get")+" url("+$("#footBgImg").attr("src")+") "+$("#footBgRepeat").val()+" "+$("#footBgPosition").val());
                    alert("设置成功！");
                }else{
                    alert("设置失败！");
                }
            }
        })
    });
    //----------------公司信息编辑相关代码--------------
    //公司信息编辑
    $("[data-target=#comProEdit]").click(function(e){
        e.preventDefault();
        $("#companyName").val($("#comName").html());
        $("#companyAddress").val($("#comAddress").html());
        $("#companyContact").val($("#comContact").html());
        $("#companyContactTel").val($("#comContactTel").html());
        $("#companyIntro").val($("#comPro").html());
    });
    //公司信息编辑保存
    $("#companyInfoSave").click(function(){
        var companyName = $("#companyName").val();
        var companyAddress  =$("#companyAddress").val();
        var companyContact = $("#companyContact").val();
        var companyContactTel = $("#companyContactTel").val();
        var companyIntro = $("#companyIntro").val();
        $.ajax({
            url:"/action/web_cmd.asp?act=web_set1",
            type:"post",
            dataType:"json",
            data:{
                companyName:companyName,
                companyAddress:companyAddress,
                companyContact:companyContact,
                companyContactTel:companyContactTel,
                companyIntro:companyIntro
            },
            success:function(data){
                if(data.status == 0){
                    $("#comName").html(companyName);
                    $("#comAddress").html(companyAddress);
                    $("#comContact").html(companyContact);
                    $("#comContactTel").html(companyContactTel);
                    $("#comPro").html(companyIntro);
                    $('#comProEdit').modal('hide');

                    $(".contactCnt").children(".company-desc").children(".company-desc-content").find("p").html(companyIntro);
                    $(".contactCnt").children(".contact-type").children(".contact-type-content").find(".the-linkman").html(companyContact);
                    $(".contactCnt").children(".contact-type").children(".contact-type-content").find(".the-phone").html(companyContactTel);
                    $(".contactCnt").children(".contact-address").children(".contact-address-content").find(".the-address").html(companyAddress);
                }else{
                    alert(data.message);
                }
            }
        })
    });
    //------------------SEO相关代码------------------
    //设置
    $("#SEOEdit").click(function () {
        $("#SEOTitle").val($("#SEOTitleShow").html());
        $("#SEOKey").val($("#SEOKeyShow").html());
        $("#SEODesc").val($("#SEODescShow").html());

        $(".SEOInfo").addClass("hidden");
        $(".SEOInfoEdit").removeClass("hidden");
    });
    $("#SEOEditBack").click(function(){
        $(".SEOInfo").removeClass("hidden");
        $(".SEOInfoEdit").addClass("hidden");
    });
    //SEO信息编辑保存
    $("#SEOInfoSave").click(function(){
        var SEOTitle = $("#SEOTitle").val();
        var SEOKey = $("#SEOKey").val();
        var SEODesc = $("#SEODesc").val();
        if(SEOTitle.length > 100){
            alert("SEO标题不能超过100个字符");
            return false;
        }
        if(SEOKey.length > 100){
            alert("SEO关键字不能超过100个字符");
            return false;
        }
        $.ajax({
            url:"/action/web_cmd.asp?act=web_set2",
            type:"post",
            dataType:"json",
            data:{
                seo_bt:SEOTitle,
                seo_key:SEOKey,
                seo_ms:SEODesc
            },
            success:function(data){
                if(data.status == 0){
                    $("#SEOTitleShow").html(SEOTitle);
                    $("#SEOKeyShow").html(SEOKey);
                    $("#SEODescShow").html(SEODesc);

                    $(".SEOInfo").removeClass("hidden");
                    $(".SEOInfoEdit").addClass("hidden");
                }else{
                    alert(data.message);
                }
            }
        })
    });
    //------------------网站分享相关代码------------------
    //设置网站分享上传图片
    $('#shareUploader').fileupload({
        url: '/action/upload/',
        dataType: 'json',
        add: function (e, data) {
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { fileinpname: 'pic', time: Date() }
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.files[0].name;
                filename = filename.substring(0,filename.length-4);
                $("#shareImg").attr("src",data.result.relatPath);
            }
        }
    });
    //网站分享编辑
    $("#shareEdit").click(function(){
        $("#shareTitle").val($("#shareTitleShow").html());
        $("#shareDesc").val($("#shareDescShow").html());

        $(".shareInfoShow").addClass("hidden");
        $(".shareInfoEdit").removeClass("hidden");
    });
    //网站分享取消
    $("#shareEditBack").click(function(){
        $(".shareInfoEdit").addClass("hidden");
        $(".shareInfoShow").removeClass("hidden");
    });
    //网站分享保存
    $("#shareSave").click(function(){
        var shareTitle = $("#shareTitle").val();
        var shareDesc = $("#shareDesc").val();
        var shareImg = $("#shareImg").attr("src");
        $.ajax({
            url:"/action/web_cmd.asp?act=web_set3",
            type:"post",
            dataType:"json",
            data:{
                shareTitle:shareTitle,
                shareDesc:shareDesc,
                sharepic:shareImg
            },
            success:function(data){
                if(data.status == 0){
                    $("#shareTitleShow").html(shareTitle);
                    $("#shareDescShow").html(shareDesc);
                    $(".shareInfoEdit").addClass("hidden");
                    $(".shareInfoShow").removeClass("hidden");

                }else{
                    alert(data.message);
                }
            }
        })
    });
    //------------------TOP Setting相关代码---------------------
    //获取top-1信息
    $("[data-target=#topSetting]").click(function(){
        $.ajax({
            url:"/action/web_cmd.asp?act=one_pageshow",
            type:'get',
            dataType:"json",
            success:function(data){
                var webLinkStr = "";
                $.each(data.gwlj,function(i,val){
                    webLinkStr += "<tr>"+
                                        "<td><input type='text' class='form-control' placeholder='请输入链接名称' value='"+val.ljname+"' /></td>"+
                                        "<td><input type='text' class='form-control' placeholder='请输入链接地址' value='"+val.ljdz+"' /></td>"+
                                    "</tr>";
                });
                $("#webLinkList").html(webLinkStr);
                $("#webName").val(data.gwmc);
                $("#webPath").val(data.gwurl);
                //top-1
                $("#TOP1").find(".topName").val(data.top1.biaoti);
                $("#TOP1").find(".full").spectrum("set",data.top1.bg);
                $.each(data.top1.tustr,function(i,val){
                    $("#topPic1-"+(i+1)).attr("src",val.pic);
                    $("#topPicName1-"+(i+1)).val(val.tuname);
                    $("#topPicUrl1-"+(i+1)).val(val.url);
                });
                //top2
                $("#TOP2").find(".topName").val(data.top2.biaoti);
                $("#TOP2").find(".full").spectrum("set",data.top2.bg);
                $.each(data.top2.tustr,function(i,val){
                    $("#topPic2-"+(i+1)).attr("src",val.pic);
                    $("#topPicName2-"+(i+1)).val(val.tuname);
                    $("#topPicUrl2-"+(i+1)).val(val.url);
                });
                //top3
                $("#TOP3").find(".topName").val(data.top3.biaoti);
                $("#TOP3").find(".full").spectrum("set",data.top3.bg);
                $.each(data.top3.tustr,function(i,val){
                    $("#topPic3-"+(i+1)).attr("src",val.pic);
                    $("#topPicName3-"+(i+1)).val(val.tuname);
                    $("#topPicUrl3-"+(i+1)).val(val.url);
                });
            }
        })
    })
    //图片上传(top，全屏轮播图)
    $('.topPicUploader').fileupload({
        url: '/action/upload/',
        dataType: 'json',
        add: function (e, data) {
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { fileinpname: 'pic', time: Date() }
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.files[0].name;
                filename = filename.substring(0,filename.length-4);
                //$("#postPic").attr("src",data.result.relatPath);
                $(this).parents(".caption").siblings(".topImg").children("img").attr("src",data.result.relatPath);
            }
        }
    });
    //Top设置提交
    $("#saveTop").click(function(){
        var webInfo = $("#webName").val() + "|" + $("#webPath").val();
        var webLink = "";
        $("#webLinkList").children("tr").each(function(){
            webLink += $(this).children("td:eq(0)").children("input").val() + "," + $(this).children("td:eq(1)").children("input").val() + "|";
        });
        webLink=webLink.substring(0,webLink.length-1);
        //top1=大标题名称,背景色|图片地址,名称,链接|图片地址,名称,链接|图片地址,名称,链接 ... 6个固定图片地址,名称,链接
        var top1 = $("#TOP1").find(".topName").val() + "," + $("#TOP1").find(".full").spectrum("get") + "|" +
                    $("#topPic1-1").attr("src") + "," + $("#topPicName1-1").val() + "," + $("#topPicUrl1-1").val() + "|" +
                    $("#topPic1-2").attr("src") + "," + $("#topPicName1-2").val() + "," + $("#topPicUrl1-2").val() + "|" +
                    $("#topPic1-3").attr("src") + "," + $("#topPicName1-3").val() + "," + $("#topPicUrl1-3").val() + "|" +
                    $("#topPic1-4").attr("src") + "," + $("#topPicName1-4").val() + "," + $("#topPicUrl1-4").val() + "|" +
                    $("#topPic1-5").attr("src") + "," + $("#topPicName1-5").val() + "," + $("#topPicUrl1-5").val() + "|" +
                    $("#topPic1-6").attr("src") + "," + $("#topPicName1-6").val() + "," + $("#topPicUrl1-6").val();
        var top2 = $("#TOP2").find(".topName").val() + "," + $("#TOP2").find(".full").spectrum("get") + "|" +
                    $("#topPic2-1").attr("src") + "," + $("#topPicName2-1").val() + "," + $("#topPicUrl2-1").val() + "|" +
                    $("#topPic2-2").attr("src") + "," + $("#topPicName2-2").val() + "," + $("#topPicUrl2-2").val() + "|" +
                    $("#topPic2-3").attr("src") + "," + $("#topPicName2-3").val() + "," + $("#topPicUrl2-3").val() + "|" +
                    $("#topPic2-4").attr("src") + "," + $("#topPicName2-4").val() + "," + $("#topPicUrl2-4").val() + "|" +
                    $("#topPic2-5").attr("src") + "," + $("#topPicName2-5").val() + "," + $("#topPicUrl2-5").val() + "|" +
                    $("#topPic2-6").attr("src") + "," + $("#topPicName2-6").val() + "," + $("#topPicUrl2-6").val();
        var top3 = $("#TOP3").find(".topName").val() + "," + $("#TOP3").find(".full").spectrum("get") + "|" +
                    $("#topPic3-1").attr("src") + "," + $("#topPicName3-1").val() + "," + $("#topPicUrl3-1").val() + "|" +
                    $("#topPic3-2").attr("src") + "," + $("#topPicName3-2").val() + "," + $("#topPicUrl3-2").val() + "|" +
                    $("#topPic3-3").attr("src") + "," + $("#topPicName3-3").val() + "," + $("#topPicUrl3-3").val() + "|" +
                    $("#topPic3-4").attr("src") + "," + $("#topPicName3-4").val() + "," + $("#topPicUrl3-4").val() + "|" +
                    $("#topPic3-5").attr("src") + "," + $("#topPicName3-5").val() + "," + $("#topPicUrl3-5").val() + "|" +
                    $("#topPic3-6").attr("src") + "," + $("#topPicName3-6").val() + "," + $("#topPicUrl3-6").val();
        //官网名称|官网链接
        $.ajax({
            url:"/action/web_cmd.asp?act=one_page",
            type:"post",
            dataType:"json",
            data:{
                gwxx:webInfo,
                gwlj:webLink,
                top1:top1,
                top2:top2,
                top3:top3
            },
            success:function(data){
                if(data.status == 0){
                    $('#topSetting').modal('hide');
                }else{
                    alert(data.message);
                }
            }
        })
    });
    //----------------------------悬浮框设置相关代码-----------------------------
    //qrcode
    $('#qrcodeUploader').fileupload({
        url: '/action/upload/',
        dataType: 'json',
        add: function (e, data) {
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { fileinpname: 'pic', time: Date() }
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.files[0].name;
                filename = filename.substring(0,filename.length-4);
                $("#qrcodeImg").attr("src",data.result.relatPath);
            }
        }
    });
    //保存客服信息
    $("#customServiceSave").click(function(){
        var customBg = $("#customBg").spectrum("get") + "";
        var customName = $("#customName").val();
        var qq = $("#qqNumber").val();
        var tel = $("#telNumber").val();
        var qrCodeImg = $("#qrcodeImg").attr("src");
        var message = $("#enableMessage").is(":checked") ? 1 : 0;
        $.ajax({
            url:"/action/web_cmd.asp?act=kefu_edit",
            type:"post",
            data:{
                bgcol:customBg,
                kfname:customName,
                qq:qq,
                wxcode:qrCodeImg,
                ifly:message,
            },
            success:function(data){
                var jsondata = $.parseJSON(data);
                if(jsondata.status == 0){
                    $('#suspension').modal('hide');
                    //$(".suspension .a").css("background-color",customBg);
                }else{
                    alert(jsondata.message);
                }
            }
        })
    });
    //获取客服信息
    $("[data-target=#suspension]").click(function(){
        //$("#customBg").spectrum("set","#000000");
        $.ajax({
            url:"/action/web_cmd.asp?act=kefu_show",
            type:"get",
            success:function(data){
                var jsondata = $.parseJSON(data);
                $("#customBg").spectrum("set",jsondata.bgcol);
                $("#customName").val(jsondata.kfname);
                $("#qqNumber").val(jsondata.qq);
                $("#qrcodeImg").attr("src",jsondata.wxcode);
                if(jsondata.ifly == 1){
                    $("#enableMessage").attr("checked",true);
                }else{
                    $("#enableMessage").attr("checked",false);
                }
            }
        })
    })
    //更换模板
    // $(document).on("click",".templetImg",function(){

    // });
    //切换使用模板
    $(document).on("click",".templetUsed",function(){
        var siteid = $(this).attr("data-id");
        $.ajax({
            url:"/action/web_cmd.asp?act=copy_website",
            type:"post",
            dataType:"json",
            data:{
                siteid:siteid
            },
            success:function(data){
                if(data.status == 0){//成功

                }else{//失败
                    alert(data.message);
                }
            }
        })
    });
    //导航页面切换
    $(document).on("click","#nav li a",function(){
        clearInterval(timer);
        $(this).parent("li").addClass("active").siblings("li").removeClass("active");
        $(this).parent("li").css("background",webColor).siblings("li").css("background","#000");

        pageID = $(this).attr("data-id");
        restoreData();
        dragComponent();
        timer = setInterval(function () {
            handleSaveLayout()
        }, timerSave);
        scrollTxt();
        $("#pageBox").find(".leftMainItem").eq($(this).parent("li").index()).children('a').addClass("pageAct");
        $("#pageBox").find(".leftMainItem").not(":eq("+$(this).parent('li').index()+")").children('a').removeClass("pageAct");
        showFullCarouse(pageID);

    });
    //页面切换
    $(document).on("click","#pageBox .leftMainItem>a",function(e){
        e.preventDefault();
        clearInterval(timer);
        $("#pageBox").find(".leftMainItem").children("a").removeClass("pageAct");
        $(this).addClass("pageAct");
        pageID = $(this).parent(".leftMainItem").attr('data-id');
        restoreData();
        dragComponent();
        timer = setInterval(function () {
            handleSaveLayout()
        }, timerSave);
        scrollTxt();
        var index = 0;
        if($(this).parent().parent().hasClass("leftMainList")){
            index = $(this).parent().index() + 1;
        }
        $("#nav li").eq(index).addClass("active").siblings("li").removeClass("active");
        $("#nav li").eq(index).css("background",webColor).siblings("li").css("background","#000");
        showFullCarouse(pageID);
    });
    //保存页面
    $("#button-download-modal").click(function (e) {
        e.preventDefault();
        var count = 0;
        for(var i = 0; i < pageIDsArr.length; i++){
            cleanHtmlObj[pageIDsArr[i]] = downloadLayoutSrc(layoutdataObj[pageIDsArr[i]]);
            $.ajax({
                url:"/action/web_cmd.asp?act=page_save",
                type:"post",
                dataType:"json",
                async:false,
                data:{
                    pageid:pageIDsArr[i],
                    cleanHtml:cleanHtmlObj[pageIDsArr[i]],
                    oldhtml:layoutdataObj[pageIDsArr[i]]
                },
                success:function(data){
                    if(data.status == 0){
                        count++;
                    }
                }
            })
        }
        if(count == pageIDsArr.length){
            $("#saveInfo").removeClass("hidden");
            setTimeout(function(){
                $("#saveInfo").addClass("hidden");
            },3000)
        }
    });
    //编辑打开(单段文字，滚动文字)
    $('body.edit .demo').on("click","[data-target=#editTxt]",function(e) {
        e.preventDefault();
        if($(this).parent().parent().find('.view').find('.marquee').length > 0){//编辑文字滚动
            for(var i = 0; i < marqueeIdArr.length; i++){
                clearInterval(marqueeIdArr[i]);
            }
            currenteditor = $(this).parent().parent().find('.view').find('.marqueeHidden');
        }else{
            currenteditor = $(this).parent().parent().find('.view').find('.txt');
        }

        pTitle = $(this).parent().parent().find('.view').find('.pTitle');
        pSubTitle = $(this).parent().parent().find(".view").find('.pSubTitle');
        titleHide = $(this).parent().parent().find(".view").find('.pTitleBox');
        var eText = currenteditor.html();
        eText = $.trim(eText);
        editor.setValue(eText);
        $("#projectTitle").val(pTitle.html());
        $("#projectSubTitle").val(pSubTitle.html());
        //是否现实项目标题
        if(titleHide.hasClass('hidden')){
            $("#titleHide").attr("checked",true);
        }else{
            $("#titleHide").attr("checked",false);
        }
        //手机是否显示标题
        if(titleHide.hasClass('hidden-xs')){
            $("#mobileHide").attr("checked",true);
        }else{
            $("#mobileHide").attr("checked",false);
        }
    });
    //编辑内容保存(单段文字，滚动文字)
    $("#saveTxt").click(function(e) {
        e.preventDefault();
        pTitle.html($("#projectTitle").val());
        pSubTitle.html($("#projectSubTitle").val());
        if($("#titleHide").is(":checked")){
            titleHide.addClass("hidden");
        }else{
            titleHide.removeClass("hidden");
        }
        if($("#mobileHide").is(":checked")){
            titleHide.addClass("hidden-xs");
        }else{
            titleHide.removeClass(("hidden-xs"));
        }
        currenteditor.html(editor.getValue());
        if(currenteditor.parent('.view').find('.marquee').length > 0){
            currenteditor.parent('.view').find('.marquee').remove('.marquee');
            currenteditor.parent('.view').append('<div class="marquee"><div class="txt">'+currenteditor.parent('.view').find('.marqueeHidden').html()+'</div></div>')
            currenteditor.parent('.view').find('.marquee').kxbdMarquee({direction:"up",isEqual:false});
        }
    });

    $("#download").click(function () {
        downloadLayout();
        return false
    });
    $("#downloadhtml").click(function () {
        downloadHtmlLayout();
        return false
    });
    //编辑
    $("#edit").click(function () {
        $("body").removeClass("devpreview sourcepreview");
        $("body").addClass("edit");
        //$(".demo").removeClass("container");
        removeMenuClasses();
        $(".suspension").width($("body").width() - 300);
        return false
    });
    //预览
    // $("#sourcepreview").click(function () {
    //     $("body").removeClass("edit");
    //     $("body").addClass("devpreview sourcepreview");
    //     //$(".demo").addClass("container");
    //     removeMenuClasses();
    //     $("#handle").removeClass("on");
    //     $(".suspension").width($("body").width() + 300);
    //     return false
    // });
    //清空
    $("#clear").click(function (e) {
        e.preventDefault();
        if(confirm("确定清空网站内容吗？")){
            clearDemo()
        }
    });
    //预览切换
    $("#handle").click(function(){
        if($(this).hasClass("on")){
            $("body").removeClass("edit");
            $("body").addClass("devpreview sourcepreview");
            //$(".demo").addClass("container");
            removeMenuClasses();
            $(this).removeClass("on");
            $(".suspension").width($("body").width() + 300);
            return false
        }else{
            $("body").removeClass("devpreview sourcepreview");
            $("body").addClass("edit");
            //$(".demo").removeClass("container");
            removeMenuClasses();
            $(this).addClass("on");
            $(".suspension").width($("body").width() - 300);
            return false
        }

    });
    // $("#devpreview").click(function () {
    //     $("body").removeClass("edit sourcepreview");
    //     $("body").addClass("devpreview");
    //     removeMenuClasses();
    //     $(this).addClass("active");
    //     return false
    // })
    $(".nav-header").click(function () {
        $(".sidebar-nav .boxes, .sidebar-nav .rows").hide();
        $(this).next().slideDown()
    });
    removeElm();
    configurationElm();
    gridSystemGenerator();

})